<template>
  <div class="body">
    <div class="table_content">
      <div class="table-card">
        <el-table
          class="table"
          :data="tableData.data"
          :row-class-name="tableRowClassName"
          v-loading="loading.table"
          :border="true"
        >
          <el-table-column prop="id" label="ID" width="60"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-link
                class="handle"
                type="warning"
                v-show="scope.row.status == 'stop'"
                @click="handleTableRegain(scope.$index, scope.row)"
                >恢复</el-link
              >
              <el-link
                class="handle"
                type="primary"
                v-show="scope.row.status == 'normal'"
                @click="handleTableEdit(scope.$index, scope.row)"
                >编辑</el-link
              >
              <el-link
                class="handle"
                type="danger"
                v-show="scope.row.status == 'normal'"
                @click="handleTableDelete(scope.$index, scope.row)"
                >删除</el-link
              >
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          @size-change="handleTableSizeChange"
          @current-change="handleTableCurrentChange"
          :current-page="currentPage4"
          :page-sizes="$pageSize"
          :page-size="tableParams.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {
  apiProductlistpage,
  apiProductUpdatePart,
  apiProductDelete,
} from "@/api/apiCommon";
import { paramsDeleteNull } from "@/utils/common";
import { assignValueToList, processTableParams } from "@/utils/appUtil";
export default {
  name: "Index",
  data() {
    return {
      tableParams: {
        page: 1,
        size: 15,
        time_interval: [
          new Date(new Date().setHours(0, 0, 0, 0)), // 今天的初始时间戳
          new Date(new Date().setHours(23, 59, 59, 999)), // 今天的结束时间戳
        ],
      },
      tableData: {
        current_page: 1,
        data: [],
        last_page: 1,
        per_page: "15",
        total: 0,
      },
      currentPage4: 1,
      business_select: [],
      organization_select: [],
      fromViewCofing: {
        input_width: "120px",
        label_width: "120px",
      },
      loading: {
        dialog_create: false,
        table: false,
        form: false,
      },
    };
  },
  created() {
    this.loadTableData();
  },
  methods: {
    // 列表请求
    loadTableData() {
      const _this = this;
      _this.loading_start("table");
      var tableParamsData = processFormData({ ..._this.tableParams });
      apiProductlistpage(tableParamsData)
        .then((response) => {
          const result = response;
          var datalist = result.data.datalist;
          assignValueToList(datalist.data).then((response) => {
            datalist.data = response;
            _this.tableData = datalist;
            _this.loading_end("table");
          });
        })
        .catch(function (result) {
          _this.tableData.data = null;
          _this.loading_end("table");
        });
    },
    // 页面跳转
    handleToGoCreate() {
      this.$router.push({ path: "/product/create", query: { id: 0 } });
    },
    // 表格行状态
    tableRowClassName({ row, rowIndex }) {
      if (row["status"] == "stop") {
        return "discard-row";
      }
    },
    // 表格操作
    handleTableEdit(index, row) {
      this.$router.push({ path: "/product/update", query: { id: row.id } });
    },
    // 表格恢复
    handleTableRegain(index, row) {
      var _this = this;
      var params = {
        id: row.id,
        status: "normal",
      };
      _this.loading_start("table");
      apiProductUpdatePart(params).then((response) => {
        _this.loading_end("table");
        _this.loadTableData();
      });
    },
    // 表格删除
    handleTableDelete(index, row) {
      const _this = this;
      _this.loading_start("table");
      apiProductDelete({
        id: row.id,
      }).then((response) => {
        this.$message({
          message: response.msg,
          type: "success",
        });
        _this.loading_end("table");
        _this.loadTableData();
      });
    },
    // 数量改变
    handleTableSizeChange(val) {
      this.tableParams.size = val;
      this.loadTableData();
    },
    // 页数改变
    handleTableCurrentChange(val) {
      this.tableParams.page = val;
      this.loadTableData();
      // console.log(`当前页: ${val}`);
    },
    // 部分状态改变
    tableUpdateItem(item) {
      var _this = this;
      var params = {
        id: item.id,
        push_status: item.push_status,
        entry_status: item.entry_status,
      };
      _this.loading_start("table");
      apiProductUpdatePart(params).then((response) => {
        _this.loading_end("table");
      });
    },
    // 加载开始...
    loading_start(keys) {
      if (Array.isArray(keys)) {
        keys.forEach((key) => {
          this.loading[key] = true;
          setTimeout(() => {
            this.loading[key] = false;
          }, 5000);
        });
      } else {
        this.loading[keys] = true;
        setTimeout(() => {
          this.loading[keys] = false;
        }, 5000);
      }
    },
    // 加载结束...
    loading_end(keys) {
      if (Array.isArray(keys)) {
        keys.forEach((key) => {
          this.loading[key] = false;
        });
      } else {
        this.loading[keys] = false;
      }
    },
  },
};
</script>

<style lang="scss"></style>